package com.squirrel.michale;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/23 8:52 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode1765 {


    public int[][] highestPeak(int[][] isWater) {
        int[][] directions = new int[][]{{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
        if (isWater == null || isWater.length == 0) {
            return new int[][]{};
        }
        Queue<int[]> queue = new LinkedList<>();
        int m = isWater.length;
        int n = isWater[0].length;
        int[][] result = new int[m][n];
        for (int i = 0; i < m; i++) {
            Arrays.fill(result[i],-1);
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (isWater[i][j] == 1) {
                    queue.offer(new int[]{i, j});
                    result[i][j] = 0;
                }
            }
        }
        int height = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int[] temp = queue.poll();
                int x = temp[0];
                int y = temp[1];
                for (int[] dir:directions) {
                    int cur_x = x + dir[0];
                    int cur_y = y + dir[1];
                    if (cur_x < 0 || cur_x >= m || cur_y < 0 || cur_y >=n) {
                        continue;
                    }
                    if (result[cur_x][cur_y] != -1) {
                        continue;
                    }
                    result[cur_x][cur_y] = height + 1;
                    queue.offer(new int[]{cur_x, cur_y});
                }
            }
            height++;
        }
        return result;
    }

    public int[][] highestPeak2(int[][] isWater) {
        int[][] dirs = new int[][]{{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
        if (isWater == null || isWater.length == 0) {
            return new int[][]{};
        }
        Queue<int[]> queue = new LinkedList<>();
        int m = isWater.length;
        int n = isWater[0].length;
        int[][] result = new int[m][n];
        for (int i = 0; i < m; i++) {
            Arrays.fill(result[i],-1);
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (isWater[i][j] == 1) {
                    queue.offer(new int[]{i, j});
                    result[i][j] = 0;
                }
            }
        }

        int h = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int[] temp = queue.poll();
                int x = temp[0];
                int y = temp[1];
                for (int[] dir:dirs) {
                    int cur_x = x + dir[0];
                    int cur_y = y + dir[1];
                    if (cur_x < 0 || cur_x >= m || cur_y < 0 || cur_y >=n) {
                        continue;
                    }
                    if (result[cur_x][cur_y] != -1) {
                        continue;
                    }
                    result[cur_x][cur_y] = h + 1;
                    queue.offer(new int[]{cur_x, cur_y});
                }
            }
            h++;
        }
        return result;
    }
}
